<?php
/* SVN FILE: $Id: accounts.php 255 2009-12-22 06:45:47Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Bank ACP - accounts
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 255 $
* @lastrevision $Date: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/bank/adminAction/accounts.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class plugin_admin_bank_accounts extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->html = marketRegistry::loadTemplate('cp_skin_plugin_bank');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'bank_bank' ) );
		
		$redirect = array(
								'search' => 'searchMember',
								'accounts' => 'listBankAccounts',
								'add' => 'addBankAccount',
								'insert' => 'insertBankAccount',
								'edit' => 'editBankAccount',
								'update' => 'updateBankAccount',
								'delete' => 'deleteBankAccount',
							);
		
		if(!$redirect[$this->request['do']])
		{
			$this->request['do'] = 'search';
		}
		
		$function = $redirect[$this->request['do']];
		$this->$function();
		
		/* Output */
		$this->registry->output->html_main .= $this->registry->output->global_template->global_frame_wrapper();
		$this->registry->output->sendOutput();			
	}
	
	private function searchMember()
	{
		$this->registry->output->html = $this->html->searchMemberForAccount();
	}
	
	private function listBankAccounts()
	{
		$member = IPSMember::load($this->request['member_name'],'all','displayname');
		if(!$member)
		{
			$this->registry->output->showError(null);
		}
		
		$accounts = array();
		$this->DB->build(array(
								'select' => '*',
								'from' => 'market_bank_accounts',
								'where' => "a_mem_id=".$member['member_id'],
								'order' => 'balance DESC',
						)	);
		$q = $this->DB->execute();
		while($account = $this->DB->fetch())
		{
			$accounts[] = $account;
		}
		
		$this->registry->output->html = $this->html->listMemberBankAccount($member, $accounts);
	}
	
	private function addBankAccount()
	{
		$account = array(
							'a_mem_id' => isset($this->request['member_id']) ? $this->request['member_id'] : 0,
							'a_name' => "",
							'balance' => 0,
							'a_interest_time' => 86400,
							'a_last_interest' => IPS_UNIX_TIME_NOW - IPS_UNIX_TIME_NOW % 60,
							'interest_rate' => 5,
							'a_deposit_fee' => 0,
							'a_withdraw_fee' => 0,
							'a_delete_fee' => 0,
						);
		
		$this->registry->output->html = $this->html->accountForm($account, 'insert', $this->lang->words['add_account_title'], $this->lang->words['add_acccount_button']);
	}
	
	private function insertBankAccount()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$member = IPSMember::load($this->request['member_name'],'all','displayname');
		if(!$member)
		{
			$this->registry->output->showError(null);
		}
		
		if(!$this->request['a_name'])
		{
			$this->registry->output->showError($this->lang->words['account_must_have_a_name']);
		}
		
		$this->request['a_last_interest'] = intval($this->request['a_last_interest']);
		$this->request['a_last_interest'] = $this->request['a_last_interest'] - $this->request['a_last_interest'] % 60;
		$this->request['a_last_interest'] = $this->request['a_last_interest'] + intval($this->request['adjust_last_time'] * 60);
		
		$account = array(
							'a_random' => numbers::rand(0,999999),
							'a_mem_id' => $member['member_id'],
							'a_name' => $this->request['a_name'],
							'a_open' => IPS_UNIX_TIME_NOW,
							'balance' => numbers::parseInt($this->request['balance']),
							'a_interest_time' => intval($this->request['a_interest_time'] * 3600),
							'a_last_interest' => intval($this->request['a_last_interest']),
							'interest_rate' => numbers::parseFloat($this->request['interest_rate']),
							'a_deposit_fee' => numbers::parseFloat($this->request['a_deposit_fee']),
							'a_withdraw_fee' => numbers::parseFloat($this->request['a_withdraw_fee']),
							'a_delete_fee' => numbers::parseInt($this->request['a_delete_fee']),
						);
		
		$this->DB->insert('market_bank_accounts',$account);
		$account['account_id'] = $this->DB->getInsertId();
		
		bankPluginFunctions::logTransaction(bankPluginFunctions::getBankAccountID($account),'create',0,$account['balance'],0);
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_account_added_log'] );
		
		$this->registry->output->global_message = $this->lang->words['bank_account_added_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=accounts&amp;do=accounts&amp;member_name=".$this->request['member_name'] );
	}
	
	private function editBankAccount()
	{
		$account = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".intval($this->request['account_id'])));
		if(!$account)
		{
			$this->registry->output->showError(null);
		}
		
		$this->registry->output->html = $this->html->accountForm($account, 'update', $this->lang->words['edit_account_title'], $this->lang->words['edit_acccount_button']);
	}
	
	private function updateBankAccount()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$this->request['account_id'] = intval($this->request['account_id']);
		$_account = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$this->request['account_id']));
		if(!$_account)
		{
			$this->registry->output->showError(null);
		}
		
		$member = IPSMember::load($this->request['member_name'],'all','displayname');
		if(!$member)
		{
			$this->registry->output->showError(null);
		}
		
		if(!$this->request['a_name'])
		{
			$this->registry->output->showError($this->lang->words['account_must_have_a_name']);
		}
		
		$account = array(
							'a_mem_id' => $member['member_id'],
							'a_name' => $this->request['a_name'],
							'balance' => numbers::parseInt($this->request['balance']),
							'a_interest_time' => intval($this->request['a_interest_time'] * 3600),
							'a_last_interest' => $_account['a_last_interest'] + intval($this->request['adjust_last_time'] * 60),
							'interest_rate' => numbers::parseFloat($this->request['interest_rate']),
							'a_deposit_fee' => numbers::parseFloat($this->request['a_deposit_fee']),
							'a_withdraw_fee' => numbers::parseFloat($this->request['a_withdraw_fee']),
							'a_delete_fee' => numbers::parseInt($this->request['a_delete_fee']),
						);
		
		$this->DB->update('market_bank_accounts',$account,"account_id=".$this->request['account_id']);
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_account_edited_log'] );
		
		$this->registry->output->global_message = $this->lang->words['bank_account_edited_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=accounts&amp;do=accounts&amp;member_name=".$this->request['member_name'] );
	}
	
	private function deleteBankAccount()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		$this->request['account_id'] = intval($this->request['account_id']);
		$_account = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$this->request['account_id']));
		if(!$_account)
		{
			$this->registry->output->showError(null);
		}
		
		$this->DB->delete('market_bank_accounts',"account_id=".$this->request['account_id']);
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['bank_account_deleted_log'] );
		
		$this->registry->output->global_message = $this->lang->words['bank_account_deleted_redirect'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=plugin&amp;section=plugin&amp;plugin=".$this->request['plugin']."&amp;action=accounts&amp;do=accounts&amp;member_name=".$this->request['member_name'] );
	}
}